package com.hunlian.makelove.activity;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Build;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import com.hunlian.makelove.AppApplication;
import com.hunlian.makelove.R;
import com.hunlian.makelove.common.AppConfig;
import com.hunlian.makelove.net.NetVisitCallback;
import com.hunlian.makelove.net.api.SuggestionServiceVisit;
import com.hunlian.makelove.tools.MyToast;

import java.util.Timer;
import java.util.TimerTask;

public class YourSuggestionActivity extends BaseActivity {
	
	private ImageButton mBackButton;
	private Button mSendButton;
	private TextView mTitleTextView, mNumTextView;
	private EditText mEditText;
	private ProgressDialog mProgressDialog;
	private CharSequence mContent;
	private ImageButton m_img_clear;
	private YourSuggestionActivity mActivity;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mActivity = this;
		setContentView(R.layout.activity_u_suggestion);
		findView();
		init();
	}

	private void findView() {
		mBackButton = (ImageButton) findViewById(R.id.back);
		mTitleTextView = (TextView) findViewById(R.id.title);
		mSendButton = (Button) findViewById(R.id.btn_nav_right);
		mEditText = (EditText) findViewById(R.id.et_suggest);
		mNumTextView = (TextView) findViewById(R.id.tv_num);
		m_img_clear = (ImageButton)findViewById(R.id.img_clearButton);
	}
	
	private void init() {
		m_img_clear.setVisibility(View.GONE);
		m_img_clear.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				mEditText.setText("");
			}
		});
		
		mTitleTextView.setText("意见反馈");
		mSendButton.setText("提交");
		mSendButton.setVisibility(View.VISIBLE);
		mBackButton.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				finish();
			}
		});
		mSendButton.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				if (mContent == null || mContent.length() == 0) {
					Toast.makeText(YourSuggestionActivity.this, "请填写您的意见！", Toast.LENGTH_SHORT).show();
					return;
				}else{
					request();
				}

			}
		});
		mEditText.addTextChangedListener(new TextWatcher() {
			
			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {
				mContent = s;
			}
			
			@Override
			public void beforeTextChanged(CharSequence s, int start, int count, int after) {
				
			}
			
			@Override
			public void afterTextChanged(Editable s) {
				if (s.length() > 0) {
					m_img_clear.setVisibility(View.VISIBLE);
				} else {
					m_img_clear.setVisibility(View.GONE);
				}
				
				//字数不超过140字
				int num = 140;
				if (mContent.length() > num) {
				    mEditText.setText(mContent.subSequence(0, 140));
				    mEditText.setSelection(num);//设置光标在最后
				}
//				mNumTextView.setText("您还可以输入"+(140 - mContent.length())+"个字");
				mNumTextView.setText((140 - mContent.length())+"");
			}
		});
		showInputMethod();
	}

	private void request() {

		if (!AppApplication.networkAvailable) {
			MyToast.showShort("没有网络");
			return;
		}

		createProgressDialog(mActivity);
		progressDialog.show();


		SuggestionServiceVisit visit = new SuggestionServiceVisit(mActivity);
		visit.setParamters(AppConfig.getInstance().getLoginedUserId(),
				mEditText.getText().toString());
		//System.out.println("意见反馈请求参数：" + visit.getParamter());
		visit.getResult(new NetVisitCallback<String,Object>(mActivity) {

			@Override
			public void onSuccess(String data) {
				if (progressDialog != null && progressDialog.isShowing()) {
					progressDialog.dismiss();
				}
				Toast.makeText(mActivity, "提交成功", Toast.LENGTH_SHORT).show();
				mActivity.finish();

			}

			@Override
			public void onFailure(String errorEvent, String message,Object obj) {
				super.onFailure(errorEvent, message,obj);
				if (progressDialog != null && progressDialog.isShowing()) {
					progressDialog.dismiss();
				}
			}

		});

	}
	
	/**弹出软键盘*/
	private void showInputMethod() {
		mEditText.setFocusable(true);
		mEditText.setFocusableInTouchMode(true);
		mEditText.requestFocus();
		//延迟弹出输入法
		Timer timer = new Timer();
		timer.schedule(new TimerTask() {
			
			@Override
			public void run() {
				InputMethodManager imm = (InputMethodManager) mEditText.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
				imm.showSoftInput(mEditText, 0);
			}
		}, 200);
	}
	
	/**软件版本*/
	private String getVersion() {
		PackageInfo info = null;
		try {
			info = getPackageManager().getPackageInfo("com.xiuyou.jiuzhai", PackageManager.GET_CONFIGURATIONS);
		} catch (NameNotFoundException e) {
			e.printStackTrace();
		}
		return info == null ? "1.0.0" : info.versionName;
	}
	
	/**唯一的设备ID，GSM手机的 IMEI 和 CDMA手机的 MEID*/
	private String getImei() {
		TelephonyManager tm = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
		String id = tm.getDeviceId();
		return id == null ? "" : id;
	}
	
	/**设备型号*/
	private String getDevice() {
		String type = Build.MODEL;
		return type == null ? "" : type;
	}

}
